package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    PermissionDao permissionDao;

    /**
     * 弹窗展示权限列表
     * @return
     */
    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }

    /**
     * 弹窗回显选中的权限
     * @param id
     * @return
     */
    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    /**
     * 增
     * @param permission
     */
    @Override
    public void save(Permission permission) {
        permissionDao.save(permission);
    }

    /**
     * 分页查
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult queryPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Permission> page=permissionDao.findPage();
        return new PageResult(page.getTotal(),page);
    }

    /**
     * 改
     * @param permission
     */
    @Override
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }

    /**
     * 删
     * @param id
     */
    @Override
    public void delById(Integer id) {
        long count=permissionDao.queryRelation(id);
        if (count > 0) {
            throw new RuntimeException("权限表被角色表关联了，不可删除");
        }
        permissionDao.delById(id);
    }
}
